#include <stdio.h>
#include <stdlib.h>

#include "list.h"
/*
 * 创建带头结点的空链表
 * */
list *list_create()
{
	list *me;
	me = malloc(sizeof(*me));
	if(me == NULL)
		return NULL;

	me->next = NULL;
	return me;
}
/*
 * 按位置进行插入 位置计数:忽略头结点，头结点后第一个结点为0 (！采用！)||  头结点为0 
 * i 插入位置
 * data 插入数据
 * */
int list_insert_at(list *me,int i,datatype *data)
{
	if (i<0)
	{
		return -1; /* 位置错误*/
	}

	list *temp = me;
	int j = 0; /*结点位置的下标*/
	while(temp->next != NULL && j<i)
	{
		temp = temp->next;
		j++;
	}

	list *new_node;
	new_node = malloc(sizeof(*new_node));
	if(new_node == NULL)
	{
		return -2;/* 新结点创建失败*/
	}
	new_node->data = *data;/*新结点创建 数值*/

	if(temp->next == NULL)
	{
		temp ->next = new_node;
		new_node->next = NULL;/*尾结点追加*/
	}
	else
	{
		new_node->next = temp->next;
		temp->next = new_node;/*非尾结点追加*/
	}
	return 0;
}
/*
 * 按数值小到大进行插入
 * */
int list_order_insert(list *me,datatype *data)
{
#if 0
	list *temp = me,*temp2 = NULL;
	list *new_node = NULL;
	while(temp)
	{
		temp2 = temp->next;
		if (temp2)
		{
			if(*data > temp->data &&  *data < temp2->data )
			{
				new_node = malloc(sizeof(*new_node));
				if (new_node == NULL)
				{
					return -2;/* new_node创建失败*/
				}
				new_node->data = *data;
				new_node->next = temp->next;
				temp->next = new_node;
			}/*中间插入*/
		}/*前后都存在*/
		else
		{
			new_node = malloc(sizeof(*new_node));
			if(new_node == NULL)
			{
				return -2;
			}
			new_node->data = *data;
			new_node->next = NULL;
			temp->next = new_node;
		}/*只存在前*/
		temp = temp->next;
	}
	return 0;
#endif
	list *temp = me, *temp2;
	while(temp->next && temp->next->data < *data)
		temp = temp->next;
	temp2 = malloc(sizeof(*temp2));
	if(temp2 == NULL)
		return -2;

	temp2->data = *data;
	temp2->next = temp->next;
	temp->next = temp2;
	
	return 0;	
}
/*
 * 按位置删除
 * */
int list_delete_at(list *me,int i,datatype *data)
{
	*data  = -1;/**/
	if (i<0)
	{
		return -1;/*位置错误*/
	}

	int j = 0;
	list *temp = me,*temp2;

	while(j<i && temp)
	{
		temp = temp->next;
		j++;
	}/*找到前继*/

	if(!temp)
		return -2;/*没有找到下标为i*/
	else
	{
		temp2 = temp->next;
		temp->next = temp2->next;
		*data = temp2->data;
		free(temp2);
		temp2 = NULL;
	}
	return 0;
}

int list_delete(list *me,datatype *data)
{
	list *temp = me,*temp2;
	while(temp->next && temp->next->data != *data )
	{
		temp = temp->next;	
	}
	if(temp->next == NULL)
		return -1;/* 没找到对应data值*/
	else/* temp->next->data == *data*/
	{
		temp2 = temp->next;
		temp->next = temp2->next;
		free(temp2);
		temp2 = NULL;
	}
	return 0;
}

/* 
 * 链表为空吗？
 * */
int list_isempty(list *me)
{
	if(me->next == NULL)
		return 1;
	return 0;	
}
/* 
 * 打印链表
 * */
void list_display(list *me)
{
	if( list_isempty(me) == 1)
		return ;

	list *temp = me->next;
	while(temp)
	{
		printf("%d ",temp->data);
		temp = temp->next;
	}/* temp 等价于 temp != NULL*/
	printf("\n");
	return ;
}
/* 
 * 销毁链表
 * */
void list_destroy(list *me)
{
	if(me->next != NULL)
	{
	/*	list *temp = me->next,*temp2=NULL; 内存泄露了*/
		list *temp = me,*temp2=NULL;
		while(temp->next != NULL)
		{
			temp2 = temp->next;
			free(temp);
			temp = temp2;
		}
	}
	free(me);
}
